window.current_event_id = -1;
window.current_event_datetime = 0;
window.current_event_title = '';

window.next_event_id = -1;
window.next_event_datetime = 0;
window.next_event_title = '';

function resize() {
    $('#header1 h1').fit();
    $('#header2 h1').fit();
    
    $('#counter1 p').fit();
    $('#counter2 p').fit();
    
    $('#footer table td.middle p').css('font-size', Math.floor((24/1080)*$(document).height()))
            
    $('#footer table td.left img').height($('#footer').height() - 10);
    if ($('#slideshow')) {
        $('#slideshow').height($('#footer').height() - 10);
        $('#slideshow img').height($('#slideshow').height());
        $('#slideshow div').height($('#slideshow').height());
        $('#slideshow div').width($('#slideshow').width());
    }
}

function secondsToTime(secs) {
    var hours = Math.floor(secs / (60 * 60));
    
    var divisor_for_minutes = secs % (60 * 60);
    var minutes = Math.floor(divisor_for_minutes / 60);
    
    var divisor_for_seconds = divisor_for_minutes % 60;
    var seconds = Math.ceil(divisor_for_seconds);
    
    var obj = {'h': hours, 'm': minutes, 's': seconds};
    return obj;   
}

function updateTime() {
    var d = new Date();
    var current_hour = d.getHours();
    var current_minute = d.getMinutes();
    var current_second = d.getSeconds();
    
    if (current_hour < 10) current_hour = '0' + current_hour;
    if (current_minute < 10) current_minute = '0' + current_minute;
    if (current_second < 10) current_second = '0' + current_second;
    
    $('#counter1 p').html(current_hour + ':' + current_minute + ':' + current_second);    
}

function updateCountdown() {
    if (current_event_id == -1) {
        if ($('#header2 h1.left').html() != '' || $('#counter2 p').html() != '') {
            $('#header2 h1.left').html('');
            $('#counter2 p').html('');
        }
    }
    else if(current_event_id == -2) {
        if ($('#header2 h1.left').html() != 'No more events scheduled') {
            $('#header2 h1.left').html('No more events scheduled');
            $('#counter2 p').html('');
            resize();
        }
    }
    else {
        var now = new Date();
        var now_unixtime = Math.round(now.getTime() / 1000);
        var diff = current_event_datetime - now_unixtime;
        
        if (diff > 0) {
            var result = secondsToTime(diff);
            
            var hour = result.h;
            var minute = result.m;
            var second = result.s;
            
            if (hour < 10) hour = '0' + hour;
            if (minute < 10) minute = '0' + minute;
            if (second < 10) second = '0' + second;
                    
            $('#counter2 p').html(hour + ':' + minute + ':' + second);
        }
        else {
            $('#counter2 p').html('00:00:00');
        }

        if ($('#header2 h1.left').html() != current_event_title) {
            $('#header2 h1.left').html(current_event_title);
            resize();
        }
    }
}

function setNextMessage() {
    var current_next_header = $('#header2 h1.right').text();
       
    if (next_event_id == -1) {
        if (current_next_header != '') {
            $('#header2 h1.right').text('');
        }
    }
    else {
        var d = new Date(next_event_datetime * 1000);
        var n = new Date();
        
        var textdt = '';
        textdt += (d.getHours() > 9) ? d.getHours() : '0' + d.getHours();
        textdt += ':'
        textdt += (d.getMinutes() > 9) ? d.getMinutes() : '0' + d.getMinutes();
        
        if (d.getDate() != n.getDate() || d.getMonth() != n.getMonth() || d.getFullYear() != n.getFullYear()) {
            textdt += ' ';
            if (d.getDay() == 0) textdt += 'Sunday';
            else if (d.getDay() == 1) textdt += 'Monday';
            else if (d.getDay() == 2) textdt += 'Tuesday';
            else if (d.getDay() == 3) textdt += 'Wednesday';
            else if (d.getDay() == 4) textdt += 'Thursday';
            else if (d.getDay() == 5) textdt += 'Friday';
            else if (d.getDay() == 6) textdt += 'Saturday';           
        }
        
        var message = next_event_title + ' ' + textdt;
        if (current_next_header != message) {
            $('#header2 h1.right').text(message);
        }
    }
}

function checkForEvent() {
    $.ajax({
        url: 'latest.json',
        dataType: 'json',
        success: function (json) {
            if (json.result == true) {
                current_event_id = json.current.id;
                current_event_datetime = json.current.datetime;
                current_event_title = json.current.title;
                
                if (json.next) {
                    next_event_id = json.next.id;
                    next_event_title = json.next.title;
                    next_event_datetime = json.next.datetime;
                }
                else {
                    next_event_id = -1;
                    next_event_title = '';
                    next_event_datetime = 0;
                }
            }
            else {
                current_event_id = -2;
            }
            setNextMessage();
        },
        error: function () {
            current_event_id = -1;
            $('#counter2 p').html('ERROR');
        }
    })
}

function slideSwitch() {
    var $active = $('#slideshow DIV.active');

    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

function addSponsors() {
    $.ajax({
        url: 'sponsors.json',
        dataType: 'json',
        success: function (json) {
            var slideshow = $('<div id="slideshow"></div>');
            for (var i=0; i<json.length; i++) {
                $(slideshow).append('<div><img src="rotate_images/' + json[i] + '" /></div>');
            }
            $('#footer table td.right').empty();
            $('#footer table td.right').append(slideshow);
            $('#slideshow').height($('#footer').height() - 10);
            
            slideSwitch();
            $(document).everyTime(5000, function (i) {
                slideSwitch();
            }, 0);
        },
        error: function () {
            
        }
    });
}

$(document).ready(function () {
    resize();
    
    // Current Time & Countdown
    updateTime();
    updateCountdown();
    $(document).everyTime(1000, function (i) {
        updateTime();
        updateCountdown();
    }, 0);
    
    // Countdown to Next Event
    checkForEvent();
    $(document).everyTime(15000, function (i) {
        checkForEvent();
    }, 0);
    
    addSponsors();
    
    $("a[rel^='prettyPhoto']").prettyPhoto();
    
    $(window).resize(function () {
        resize();
    })
})
